package liuliu.throughwaring.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.util.ArrayList;
import java.util.List;

import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.adapter.CommonAdapter;
import liuliu.throughwaring.adapter.ViewHolder;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.custom.model.CodeModel;
import liuliu.throughwaring.model.CompanyModel;
import liuliu.throughwaring.model.PersonModel;

/**
 * 选择单位列表信息
 * Created by Administrator on 2015/12/8.
 */
public class SearchCompanyListActivity extends BaseActivity {
    @CodeNote(id = R.id.search_company_list_toolbar)
    TToolbar toolbar;
    @CodeNote(id = R.id.search_company_list_lv)
    ListView lv;
    List<CompanyModel> list = new ArrayList<CompanyModel>();
    List<CodeModel> codeList;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_search_company_list);
        Intent intent = getIntent();
        String cardId = intent.getStringExtra("CardId");
        codeList = finalDb.findAllByWhere(CodeModel.class, "codeName='Code_CompanyType'");
        List<PersonModel> plist = finalDb.findAllByWhere(PersonModel.class, "PersonCardId='" + cardId + "'");
        for (PersonModel p : plist) {
            List<CompanyModel> clist = finalDb.findAllByWhere(CompanyModel.class, "CompanyId='" + p.getPersonCompanyId() + "'");
            if (clist.size() > 0) {
                Boolean isY = true;
                for (CompanyModel c : list) {
                    if (c.getCompanyId().equals(clist.get(0).getCompanyId())) {
                        isY = false;
                        break;
                    }
                }
                if (isY) {
                    list.add(clist.get(0));
                }
            }
        }

        CommonAdapter<CompanyModel> adapter = new CommonAdapter<CompanyModel>(this, list, R.layout.item_main_unit_search) {
            @Override
            public void convert(ViewHolder holder, CompanyModel model, int position) {
                holder.setText(R.id.unit_search_item_main_unit_name, model.getCompanyName());
                holder.setText(R.id.unit_search_item_main_unit_state, Utils.getCodeVlaue(codeList, model.getCompanyType()));
                holder.setText(R.id.unit_search_item_main_tel, model.getCompanyPhoneNumber());
                holder.setText(R.id.unit_search_item_main_create_time, model.getCompanyCreateTime());
                holder.setText(R.id.unit_search_item_main_address, model.getCompanyAddress());
            }
        };
        lv.setAdapter(adapter);
    }

    @Override
    public void initEvents() {
        toolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                finish();
            }
        });
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                mUtils.IntentPost(UnitDetailActivity.class, new Utils.putListener() {
                    @Override
                    public void put(Intent intent) {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("CompanyModel", list.get(position));
                        intent.putExtras(bundle);
                    }
                });
            }
        });
    }
}
